package com.minisoft.activity;

import io.swagger.annotations.ApiModelProperty;

import java.util.*;

public class CheckInManager {
    List<Rule> rules = new ArrayList<>();
    static class Rule {
        @ApiModelProperty(value = "打卡总次数")
        private int totalSignNumber;

        @ApiModelProperty(value = "分类地点要求打卡次数，数组中的每个元素对应一个分类，0-不限，n-具体限制的次数")
        private int[] subSignNumber;

        @ApiModelProperty(value = "可获得抽奖次数")
        private int perGetDrawNumber;

        @ApiModelProperty(value = "对应抽奖的活动ID")
        private int luckDrawId;

        @ApiModelProperty(value = "可获得次数，0-不限，n-具体限制的次数")
        private int totalGetNumber;
        public Rule(int totalSignNumber, int[] subSignNumber, int perGetDrawNumber, int luckDrawId, int totalGetNumber) {
            this.totalSignNumber = totalSignNumber;
            this.subSignNumber = subSignNumber;
            this.perGetDrawNumber = perGetDrawNumber;
            this.luckDrawId = luckDrawId;
            this.totalGetNumber = totalGetNumber;
        }

        public boolean checkRule(int[] subTotalSignNumber, int totalGetNumber) {
            //计算打卡总次数，如果小于总次数，则不满足规则
            int userTotalSignNumber = Arrays.stream(subTotalSignNumber).reduce(0, Integer::sum);
            if (userTotalSignNumber < totalSignNumber) {
                return false;
            }
            //需要考虑用户已经抽过奖的情况，先看总数是否满足（应抽奖次数要大于已抽奖次数）
            int shouldGetNumber = userTotalSignNumber/totalSignNumber;
            if (shouldGetNumber <= totalGetNumber) {
                return false;
            }

            for (int i = 0; i < subSignNumber.length; i++) {
                if (subSignNumber[i] == 0) {
                    continue;
                }
                //再看每个分类地点的次数是否满足
                int shouldGetSubNumber = subTotalSignNumber[i]/subSignNumber[i];
                //应抽奖次数要大于已抽奖次数
                if (shouldGetSubNumber <= totalGetNumber) {
                    return false;
                }
            }
            return (this.totalGetNumber == 0 || totalGetNumber < this.totalGetNumber);
        }
    }

    public void addRule(Rule rule) {
        rules.add(rule);
    }

    public List<Rule> checkApplicableRules(int[] subTotalSignNumber, int[] userStatus) {
        List<Rule> applicableRules = new ArrayList<>();
        for (int i = 0; i < rules.size(); i++) {
            Rule rule = rules.get(i);
            int totalGetNumber = userStatus[i];
            if (rule.checkRule(subTotalSignNumber, totalGetNumber)) {
                applicableRules.add(rule);
                userStatus[i] = totalGetNumber + 1;
            }
        }
        return applicableRules;
    }

    public static void main(String[] args) {
        // 初始化打卡系统和规则
        CheckInManager system = new CheckInManager();
        system.addRule(new Rule(1, new int[] {1, 0}, 500, 1636, 0));
        system.addRule(new Rule(1, new int[] {0, 1}, 300, 1637, 0));
        system.addRule(new Rule(5, new int[] {1, 2}, 100, 1638, 2));

        // 示例: 用户打卡分类2的地点
        int[] subTotalSignNumber = {1, 1};
        int[] totalGetNumber = {0, 0, 0};
        List<Rule> matchedRules = system.checkApplicableRules(subTotalSignNumber,totalGetNumber);
        for (Rule rule : matchedRules) {
            System.out.println("Matched Rule: Luck Draw ID " + rule.luckDrawId);
        }

        totalGetNumber = new int[] {2, 2,1};

        // 示例: 用户打卡分类1的地点
        subTotalSignNumber = new int[] {2, 3};
        matchedRules = system.checkApplicableRules(subTotalSignNumber,totalGetNumber);
        for (Rule rule : matchedRules) {
            System.out.println("Matched Rule: Luck Draw ID " + rule.luckDrawId);
        }

        subTotalSignNumber = new int[] {6, 4};
        matchedRules = system.checkApplicableRules(subTotalSignNumber,totalGetNumber);
        for (Rule rule : matchedRules) {
            System.out.println("Matched Rule: Luck Draw ID " + rule.luckDrawId);
        }
    }
}